﻿using CgdataBase;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Linq;
using System.Windows.Media;
using MessageBox = FolderCompare.Views.MessageBox;

namespace FolderCompare.ViewModels
{
    public class WinSettingsViewModel : BindableBase
    {
        private AppSettings Settings => AppSettings.Instance;
        public int FontSize { get; set; }
        public string FontName => Settings.FontName;

        public bool OfflineMode { get; set; }
        public FreeSql.DataType[] DatabaseTypes { get; set; }
        public FreeSql.DataType DatabaseType { get; set; }
        public string DatabaseConnectionString { get; set; }
        public string TextFileTypes { get; set; }

        public FontController.FontInfo[] FontItems { get; set; }
        public FontController.FontInfo SelectedFont { get; set; }

        public DelegateCommand GenerateDefaultConnStringCommand { get; }
        public DelegateCommand SubmitCommand { get; }

        public WinSettingsViewModel()
        {
            DatabaseTypes = new FreeSql.DataType[] { FreeSql.DataType.Sqlite, FreeSql.DataType.MySql, FreeSql.DataType.SqlServer };
            ObjectHelper.DeepCopy(Settings, this);

            FontItems = FontController.GetFontItems();
            SelectedFont = GetSelectedFont(Settings.FontName);

            GenerateDefaultConnStringCommand = new DelegateCommand(OnGenerateDefaultConnString);
            SubmitCommand = new DelegateCommand(OnSubmit);
        }

        private FontController.FontInfo GetSelectedFont(string fontName)
        {
            if (fontName.IsNullOrWhiteSpace())
            {
                return null;
            }

            var items = fontName.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
            foreach (var item in items)
            {
                var font = FontItems.FirstOrDefault(s => s.FontName == item);
                if (font != null)
                {
                    return font;
                }
            }

            return null;
        }

        private void OnSubmit()
        {
            if (SelectedFont == null)
            {
                MessageBox.Show("请选择字体");
                return;
            }

            ObjectHelper.DeepCopy(this, Settings);
            Settings.FontName = SelectedFont.FontName;
            App.GetActiveWindow().DialogResult = true;
        }

        private void OnGenerateDefaultConnString()
        {
            switch (DatabaseType)
            {
                case FreeSql.DataType.MySql:
                    DatabaseConnectionString = "Data Source=127.0.0.1;Port=3306;User ID=root;Password=123456; Initial Catalog=dbName;Charset=utf8; SslMode=none;Min pool size=1";
                    break;
                case FreeSql.DataType.SqlServer:
                    DatabaseConnectionString = "Data Source=127.0.0.1;User Id=sa;Password=123456;Initial Catalog=dbName;Encrypt=True;TrustServerCertificate=True;Pooling=true;Min Pool Size=1";
                    break;
                case FreeSql.DataType.Sqlite:
                    DatabaseConnectionString = "Data Source=database.db; Pooling=true;Min Pool Size=1";
                    break;
                default:
                    break;
            }
        }
    }
}